package com.hbx.controller;

import com.hbx.dto.feedback.AddFeedbackDto;
import com.hbx.dto.feedback.FeedBackHandlerDto;
import com.hbx.dto.feedback.SearchListDto;
import com.hbx.service.FeedbackService;
import com.hbx.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbx
 * @since 2022-01-29
 */
@RestController
@Api(tags = "反馈处理器")
@RequestMapping("/feedback")
public class FeedbackController {
    @Autowired
    private FeedbackService feedbackService;

    /**
     * 获取反馈类型
     * @return
     */
    @RequestMapping(value = "/getType",method = RequestMethod.GET)
    @ApiOperation("获取反馈类型")
    public Result getType(){
        Result r = new Result();
        feedbackService.getType(r);
        return r;
    }

    /**
     * 获取反馈详情
     * @return
     */
    @RequestMapping(value = "/getFeedbackDetail",method = RequestMethod.GET)
    @ApiOperation("获取反馈详情")
    public Result getFeedbackDetail(@RequestParam("id") Integer id){
        Result r = new Result();
        if (id == null){
            r.setCode(400).setMsg("入参错误");
            return r;
        }
        feedbackService.getFeedbackDetail(id, r);
        return r;
    }

    /**
     * 新增反馈
     * @return
     */
    @RequestMapping(value = "/addFeedback",method = RequestMethod.POST)
    @ApiOperation("新增反馈")
    public Result addFeedback(@RequestBody @Valid AddFeedbackDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        feedbackService.addFeedback(dto, r);
        return r;
    }

    /**
     * 处理反馈
     * @return
     */
    @RequestMapping(value = "/handlerFeedback",method = RequestMethod.POST)
    @ApiOperation("处理反馈")
    public Result handlerFeedback(@RequestBody @Valid FeedBackHandlerDto dto, BindingResult result){
        Result r = new Result();
        if (result.hasErrors()){
            r.setCode(400).setMsg(result.getFieldError().getDefaultMessage());
            return r;
        }
        feedbackService.handlerFeedback(dto, r);
        return r;
    }

    /**
     * 获取反馈列表
     * @return
     */
    @RequestMapping(value = "/getFeedbackList",method = RequestMethod.POST)
    @ApiOperation("获取反馈列表")
    public Result getFeedbackList(@RequestBody SearchListDto dto){
        Result r = new Result();
        if (dto.getPageNum() == null || dto.getPageNum() < 0){
            r.setCode(400).setMsg("pageNum错误");
            return r;
        }
        if (dto.getPageSize() == null || dto.getPageSize() < 0){
            r.setCode(400).setMsg("pageSize错误");
            return r;
        }
        feedbackService.getFeedbackListPage(dto, r);
        return r;
    }

    /**
     * 获取我的反馈列表
     * @return
     */
    @RequestMapping(value = "/getMyFeedbackList",method = RequestMethod.GET)
    @ApiOperation("获取我的反馈列表")
    public Result getMyFeedbackList(){
        Result r = new Result();
        feedbackService.getMyFeedbackList(r);
        return r;
    }
}

